package Review;


public class Test27 
{

	public static void main(String[] args) 
	{
		BinaryTreeNode node1=new BinaryTreeNode(10);
		BinaryTreeNode node2=new BinaryTreeNode(5);
		BinaryTreeNode node3=new BinaryTreeNode(12);
		BinaryTreeNode node4=new BinaryTreeNode(4);
		BinaryTreeNode node5=new BinaryTreeNode(7);		
		node1.left=node2;
		node1.right=node3;
		node2.left=node4;
		node2.right=node5;
		node3.left=node3.right=null;
		node4.left=node4.right=null;
		node5.left=node5.right=null;		
		BinaryTreeNode p = min(node1);
		while(p!=null){
			System.out.print(p.value);
			System.out.print(" ");
			p=p.right;
		}
	}
     public static BinaryTreeNode min(BinaryTreeNode root){
    	 if(root==null){
    		 return null;
    	 }
    	 root.left=max(root.left);
    	 if(root.left!=null){
    		 root.left.right=root;
    	 }
    	 root.right=min(root.right);
    	 if(root.right!=null){
    		 root.right.left=root;
    	 }
    	 while(root.left!=null){
    		 root=root.left;
    	 }
    	 return root;
     }
     public static BinaryTreeNode max(BinaryTreeNode root){
    	if(root==null){
    		return null;
    	}
    	root.left=max(root.left);
    	if(root.left!=null){
    		root.left.right=root;
    	}
    	root.right=min(root.right);
    	if(root.right!=null){
    		root.right.left=root;
    	}
    	while(root.right!=null){
    		root=root.right;
    	}
    	return root;
    	
     }



}	
